package doantotnghiep.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import doantotnghiep.business.MeasureUnitBusiness;
import doantotnghiep.model.MeasureUnit;
import doantotnghiep.modelDTO.MeasureUnitDTO;

@Controller
public class MeasureUnitController {
	
	@Autowired
	private MeasureUnitBusiness measureUnitBusiness;
	
	private MeasureUnitDTO measureUnitDTO;
	
	private MeasureUnit measureUnit;
	
	@ModelAttribute("MeasureUnitDTO")
	public MeasureUnitDTO getMeasureUnitDTOObject() {
		return new MeasureUnitDTO();
	}

	@RequestMapping(value = "/viewMeasureUnit", method = RequestMethod.GET)
	public ModelAndView viewAllMeasureUnit() {
		List<MeasureUnit> measureUnits;
		List<MeasureUnitDTO> measureUnitDTOs = new ArrayList<MeasureUnitDTO>();
		if(measureUnitBusiness != null){
			measureUnits = measureUnitBusiness.getlistMeasureUnit();
			if(measureUnits != null){ 
				for (MeasureUnit c : measureUnits) {
					measureUnitDTO = new MeasureUnitDTO();
					
					measureUnitDTO.setId(c.getId());
					measureUnitDTO.setUnitName(c.getUnitName());
					measureUnitDTOs.add(measureUnitDTO);
				}
				Map<String, Object> listAllMeasure = new HashMap<String, Object>();
				listAllMeasure.put("measureDTOs", measureUnitDTOs);
				return new ModelAndView("viewMeasureUnit", "listAllMeasure",listAllMeasure);
			}
		}
		return new ModelAndView("viewMeasureUnit","",null);
	}

	@RequestMapping(value = "/addMeasureUnit", method = RequestMethod.GET)
	public ModelAndView addMeasure() {
		return new ModelAndView("addMeasureUnit","",null);
	}
	
	@RequestMapping(value = "/addMeasureUnit", method = RequestMethod.POST)
	public String addCustomer(HttpServletRequest request, ModelMap model,
			@ModelAttribute("MeasureUnitDTO") MeasureUnitDTO measureUnitDTO) {
		if (measureUnitDTO != null) {
			measureUnit = new MeasureUnit(); 
			measureUnit.setUnitName(measureUnitDTO.getUnitName());
			this.measureUnitBusiness.addMeasure(measureUnit);
		} 
		return "redirect:/viewMeasureUnit";
	}

	@RequestMapping(value = "/editMeasureUnit", method = RequestMethod.GET)
	public ModelAndView editMeasure(HttpServletRequest request, ModelMap model,int id) {
		measureUnit =  measureUnitBusiness.getMeasureById(id);
		if(measureUnit != null){
		   measureUnitDTO = new MeasureUnitDTO();
		   measureUnitDTO.setId(measureUnit.getId());
		   measureUnitDTO.setUnitName(measureUnit.getUnitName());
		   return new ModelAndView("updateMeasureUnit", "measureUnitDTO",  measureUnitDTO);
		}
		return new ModelAndView("viewMeasureUnit","",null);
	}

	@RequestMapping(value = "/editMeasureUnit", method = RequestMethod.POST)
	public String editMeasure(HttpServletRequest request, ModelMap model,
			@ModelAttribute(" MeasureUnitDTO")  MeasureUnitDTO  measureUnitDTO) {
		if ( measureUnitDTO != null) {
		    measureUnit = new MeasureUnit();
		    measureUnit.setId(measureUnitDTO.getId());
	        measureUnit.setUnitName(measureUnitDTO.getUnitName());
			this.measureUnitBusiness.saveMeaSure(measureUnit);
		}
		return "redirect:/viewMeasureUnit";
	}

	@RequestMapping(value = "/deleteMeasureUnit", method = RequestMethod.GET)
	public String deleteCustomer(HttpServletRequest request, ModelMap model, int id) {
		measureUnitBusiness.deleteMeasure(id);
		return "redirect:/viewMeasureUnit";
	}

}



